package com.p7.notepad.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.p7.notepad.R;
import com.p7.notepad.app.BaseActivity;
import com.p7.notepad.bean.Login;
import com.p7.notepad.bean.Success;
import com.p7.notepad.network.APIManager;
import com.p7.notepad.network.exception.ExceptionHandle;
import com.p7.notepad.network.listener.RequestListener;
import com.p7.notepad.tools.Storage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class UresActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.ures_name)
    EditText mName;
    @BindView(R.id.ures_birthday)
    TextView mBirthday;
    @BindView(R.id.ures_birthday_date)
    DatePicker mBirthdayDate;
    @BindView(R.id.ures_sex)
    RadioGroup mSex;
    @BindView(R.id.ures_sex_1)
    RadioButton mSex1;
    @BindView(R.id.ures_sex_2)
    RadioButton mSex2;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ures;
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        mSex.setOnCheckedChangeListener(this);

        Login.DataBean user = Storage.getUser();
        mName.setText(user.getNickName());
        String time = timeStampDate(String.valueOf(user.getBirthday()));
        mBirthday.setText(time);
        if (user.getSex() == 1) {
            mSex1.setChecked(true);
        } else if (user.getSex() == 2) {
            mSex2.setChecked(true);
        }
    }

    @OnClick({R.id.user_quit, R.id.ures_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_quit:
                finish();
                break;
            case R.id.ures_btn:
                submit();
                break;
        }
    }

    //字符串转时间戳
    public static String getTime(String timeString){
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date d;
        try{
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch(ParseException e){
            e.printStackTrace();
        }
        return timeStamp;
    }

    public String timeStampDate(String seconds) {
        String format = "yyyy年MM月dd日";
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    private int sex = 1;

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if (i == R.id.ures_sex_1) {
            sex = 1;
        }

        if (i == R.id.ures_sex_2) {
            sex = 2;
        }
    }

    private String name;
    private String time;
    private void submit() {
        name = mName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "昵称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        int y = mBirthdayDate.getYear();
        int m = mBirthdayDate.getMonth() + 1;
        int d = mBirthdayDate.getDayOfMonth();
        if (m > 9) {
            if (d > 9) {
                time = getTime(y + ""
                        + m + "" + d);
            } else {
                time = getTime(y + ""
                        + m + "0" + d);
            }
        } else {
            if (d > 9) {
                time = getTime(y + "0"
                        + m + "" + d);
            } else {
                time = getTime(y + "0"
                        + m + "0" + d);
            }
        }

        Map<String, String> parameters = new HashMap<>();
        parameters.put("id", String.valueOf(Storage.getId()));
        parameters.put("nickName", name);
        parameters.put("birthday", time);
        parameters.put("sex", String.valueOf(sex));
        APIManager.onPost("user/updateInfo", parameters, new RequestListener() {
            @Override
            public void onSuccess(String s) {
                Gson gson = new Gson();
                Success success = gson.fromJson(s, Success.class);
                if (success.getCode() == 1000) {
                    onUserInfo();
                } else if(success.getCode() == 2001) {
                    startActivity(LoginActivity.class);
                    finish();
                } else {
                    setToast(success.getMessage() + "请稍后重试");
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable error) {
                setToast(error.message);
            }
        });

    }

    private void onUserInfo() {
        Login.DataBean dataBean = Storage.getUser();
        dataBean.setSex(sex);
        dataBean.setNickName(name);
        dataBean.setBirthday(time);
        Storage.putUser(dataBean);
        Storage.putName(name);
        Storage.putUserName(dataBean.getId(), name);
        setToast("信息修改成功");
        finish();
    }

    private void setToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }
}
